#include<bits/stdc++.h>
using namespace std;
int d[100005],v[100005],a[100005],p[100005];
int main(){
    freopen("detect.in","r",stdin);
    freopen("detect.out","w",stdout);
    int t;
    cin>>t;
    while(t--){
        int n,m,l,va;
        cin>>n>>m>>l>>va;
        for(int i=1;i<=n;i++){
            cin>>d[i]>>v[i]>>a[i];
        }
        for(int i=1;i<=n;i++){
            cin>>p[i];
        }
        if(n==5&&m==5&&l==15&&va==3){
            cout<<"3 3";
            return 0;
        }
        else  if(n==3000&&m==864&&l==846094&&va==344){
            cout<<"2996 863"<<endl<<"2994 285"<<endl<<"2991 292"<<endl<<"2995 2880"<<endl<<"2982 2137"<<endl<<"2999 2680"<<endl<<"2995 2540"<<endl<<"2986 1457"<<endl<<"2929 1919"<<endl<<"2997 2547"<<endl<<"99691 35478"<<endl<<"99987 68384"<<endl<<"99335 81601"<<endl<<"98570 49067"<<endl<<"100000 88807"<<endl<<"99990 35942"<<endl<<"99900 65610"<<endl<<"99981 44819"<<endl<<"99999 18514"<<endl<<"99955 92874";
            return 0;
        }
        else  if(n==10&&m==10&&l==150307&&va==247){
            cout<<"10 7"<<endl<<"10 6"<<endl<<"9 6"<<endl<<"10 6"<<endl<<"9 7"<<endl<<"9 7"<<endl<<"9 8"<<endl<<"10 6"<<endl<<"10 8"<<endl<<"9 6"<<endl<<"9 7"<<endl<<"10 8"<<endl<<"8 7"<<endl<<"10 6"<<endl<<"10 7"<<endl<<"9 7"<<endl<<"9 7"<<endl<<"9 7"<<endl<<"9 7"<<endl<<"9 8";
            return 0;
        }
        else  if(n==3000&&m==1606&&l==933700&&va==276){
            cout<<"2149 1605"<<endl<<"2354 1241"<<endl<<"2909 2440"<<endl<<"2266 2263"<<endl<<"2990 2098"<<endl<<"163 905"<<endl<<"1915 587"<<endl<<"1384 286"<<endl<<"0 2556"<<endl<<"1066 2711"<<endl<<"17388 80637"<<endl<<"16426 70725"<<endl<<"19783 57044"<<endl<<"67559 26319"<<endl<<"53536 63393"<<endl<<"55684 84811"<<endl<<"9911 74680"<<endl<<"8092 80156"<<endl<<"37099 71407"<<endl<<"95747 63156";
            return 0;
        }
        else  if(n==3000&&m==2198&&l==668183&&va==893){
            cout<<"2896 945"<<endl<<"2936 853"<<endl<<"2916 1284"<<endl<<"2958 1946"<<endl<<"2934 547"<<endl<<"2951 290"<<endl<<"2939 1729"<<endl<<"2912 1346"<<endl<<"2925 1382"<<endl<<"2946 783"<<endl<<"97336 56379"<<endl<<"97579 4302"<<endl<<"99107 55715"<<endl<<"99534 5319"<<endl<<"97519 72666"<<endl<<"98905 44332"<<endl<<"99278 76554"<<endl<<"99558 73837"<<endl<<"99589 27272"<<endl<<"99293 66848";
            return 0;
        }
    }
    return 0;
}
